package com.benefm.multipar.pad.ble;

import com.benefm.multipar.pad.event.BloodPressureEvent;
import com.benefm.multipar.pad.event.EventAction;
import com.benefm.multipar.pad.event.MsgEvent;

import org.greenrobot.eventbus.EventBus;

public class BPWatchDataParse {

    //固定顺序，请勿随意调整
    public static final String[] errorCode50Msg = new String[]{"", "手势位置高", "手势位置正常", "手势位置低", "测量结果为Err", "血压手表低电", "手势位置错误"};
    //固定顺序，请勿随意调整
    public static final String[] errorCode52Msg = new String[]{"", "佩戴错误", "体动", "佩戴过紧", "佩戴过松", "漏气"};

    public static void parseData(byte[] data) {

        if ((data[2] & 0xff) == 0x02) {

            if ((data[3] & 0xff) == 0x00) { //显示压力值
                int pressureValue = data[4] & 0xff;
                System.out.println("pressure value == " + pressureValue);
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_BPW1_PRESSURE, pressureValue));
            } else if ((data[3] & 0xff) == 0x50) {
                String errorMessage = errorCode50Msg[0];
                int status = data[4] & 0xff;
                if (status > 0 && status <= 6) {
                    errorMessage = errorCode50Msg[status];
                }
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_BPW1_ERROR, errorMessage));

            } else if ((data[3] & 0xff) == 0x52) { //aa aa 21 06 5a 00 02 52 04 54 7d
                String errorMessage = errorCode52Msg[0];
                int status = data[4] & 0xff;
                if (status > 0 && status <= 5) {
                    errorMessage = errorCode52Msg[status];
                }
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_BPW1_ERROR, errorMessage));
            }

        } else if ((data[2] & 0xff) == 0x0a) {
            int sbp = data[9] & 0xff;
            int dbp = data[10] & 0xff;
            int pr = data[11] & 0xff;
            BloodPressureEvent bpEvent = new BloodPressureEvent();
            bpEvent.sbp = sbp;
            bpEvent.dbp = dbp;
            bpEvent.pr = pr;
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_BPW1_SBP_DBP, bpEvent));
        }
    }

}
